<?php
declare (strict_types = 1);

namespace ui;

/**
 * 
 */
class TableColumn {

    public const TYPE_TEXT = "TEXT";
    public const TYPE_TEXT_POPTIP = "TEXT_POPTIP";
    public const TYPE_IMAGE = "IMAGE";
    public const TYPE_DOT = "DOT";
    public const TYPE_MIXED = "MIXED";
    public const TYPE_SELECTION = "selection";
    
    private $key = '';
    private $title = '';
    private $type = '';
    private $slot = '';
    private $align = 'center';
    private $order = '';
    private $sortable = '';
    private $buttons = [];
    private $width;
    private $minWidth;
    private $textKey;
    private $colorDots;
    private $poptip_width;

    public static function create($key, $title) {
        $item = new TableColumn();
        $item->type = self::TYPE_TEXT;
        $item->key = $key;
        $item->slot = $key;
        $item->title = $title;
        return $item;
    }

    public static function createLongText($key, $title, $poptip_width = 400) {
        $item = self::create($key, $title);
        $item->type = self::TYPE_TEXT_POPTIP;
        $item->poptip_width = $poptip_width;
        return $item;
    }

    public static function createSelection() {
        $item = self::create('selection', '');
        $item->type = self::TYPE_SELECTION;
        return $item;
    }

    public static function createImage($key, $title) {
        $item = self::create($key, $title);
        $item->type = self::TYPE_IMAGE;
        $item->setMinWidth(50);
        return $item;
    }

    public static function createDot($key, $title) {
        $item = self::create($key, $title);
        $item->type = self::TYPE_DOT;
        $item->setMinWidth(50);
        return $item;
    }

    public function mapDot($key, $color, $text = null) {
        if(empty($this->colorDots)) $this->colorDots = [];
        $this->colorDots[] = [
            'key' => $key,
            'color' => $color,
            'text' => $text,
        ];
        return $this;
    }

    public function addButton(Button $button) {
        $this->buttons[] = $button;
        $this->type = self::TYPE_MIXED;
        return $this;
    }

    public function setSortable($sortable = 'custom') {
        $this->sortable = $sortable;
        return $this;
    }

    public function setOrderDesc() {
        $this->order = 'desc';
        return $this;
    }

    public function setTextKey($textKey) {
        $this->textKey = $textKey;
        return $this;
    }
    
    public function setWidth($width) {
        $this->width = $width;
        return $this;
    }

    public function setMinWidth($minWidth) {
        $this->minWidth = $minWidth;
        return $this;
    }

    public function build() {
        $buttons = [];
        foreach ($this->buttons as $btn) {
            $buttons[] = $btn->build();
        }
        $option = [
            'type' => $this->type,
            'key' => $this->key,
            'title' => $this->title,
            'slot' => $this->slot,
            'align' => $this->align,
            'order' => $this->order,
            'poptip_width' => $this->poptip_width,
            'sortable' => $this->sortable,
            'buttons' => $buttons,
        ];
        if(isset($this->minWidth)) $option['minWidth'] = $this->minWidth;
        if(isset($this->width)) $option['width'] = $this->width;
        if(isset($this->textKey)) $option['text_key'] = $this->textKey;
        if(isset($this->colorDots)) $option['dots'] = $this->colorDots;
        return $option;
    }
}
